﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Service.UserManagements;
using Service.UserManagements.Dto;
using System;

namespace UI.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserRoleController : ControllerBase
    {
        private readonly IUserRoleService userRoleService;

        public UserRoleController(IUserRoleService userRoleService)
        {
            this.userRoleService = userRoleService;
        }

        /// <summary>
        /// 用户角色显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public IActionResult Query(string userName, DateTime? sDate, DateTime? eDate, int page, int size)
        {
            var list = userRoleService.Query( userName,  sDate,  eDate,  page,  size);
            return Ok(list);
        }

        /// <summary>
        /// 图片上传
        /// </summary>
        /// <param name="formFile"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult postFile(IFormFile formFile)
        {
            string path = System.IO.Directory.GetCurrentDirectory()+@"\wwwroot\img\";

            string newfilename = System.DateTime.Now.ToString("yyyyMMddhhmmssff");

            string exp = System.IO.Path.GetExtension(formFile.FileName);

            string filePath = path + newfilename + exp;

            using (var stram = System.IO.File.Create(filePath))
            {
                formFile.CopyTo(stram); //拷贝上传的图片流
                stram.Flush();//关闭流
            }

            return Ok("img/" + newfilename + exp);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="userRoleDto"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Insert(UserRoleDto userRoleDto)
        {
            return Ok(userRoleService.Insert(userRoleDto));
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="userRoleDto"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Delete(int id, int adminRoleId)
        {
            return Ok(userRoleService.Delete(id,adminRoleId));
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult DeleteAll(int[] ids)
        {
            return Ok(userRoleService.DeleteAll(ids));
        }


        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="userRoleDto"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult QueryFirst(int id,int adminRoleId)
        {
            return Ok(userRoleService.QueryFirst(id, adminRoleId));
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="userRoleDto"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult Update(UserRoleDto userRoleDto)
        {
            return Ok(userRoleService.Update(userRoleDto));
        }
    }
}
